import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_boost/boost_navigator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hzcloud_flt/common/hz_loading/hz_loading_dialog.dart';
import 'package:hzcloud_flt/common/hz_network/hz_network.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  String _account;
  String _pwd;

  ///
  /// 点击登录
  ///
  requestLogin() {
    FormState current = _formKey.currentState;
    if (!current.validate()) return;
    current.save();
    HZNetworkConfig config = HZNetworkConfig(
      HZNetworkFetchMethod.post,
      '/a/b',
      data: {'name': _account, "pwd": _pwd},
    );
    HZLoadingDialog.showLoading(msg: "登录中...");
    Future.delayed(Duration(seconds: 1), () {
      HZLoadingDialog.dismiss();
      BoostNavigator.instance.pop();
    });
    HZNetwork.request(config).then((value) {
      HZLoadingDialog.dismiss();
    }).catchError((e) {
      HZLoadingDialog.dismiss();
      HZLoadingDialog.showError('请求失败');
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print(Size.fromHeight(151 * ScreenUtil().screenWidth / 375.0));
    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: true,
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(80 * ScreenUtil().screenWidth / 375.0),
        child: Container(
          height: 151 * ScreenUtil().screenWidth / 375.0,
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
                    'assets/images/loginTop.png',
                  ),
                  fit: BoxFit.fill)),
          child: Stack(
            children: [
              Positioned(
                child: Image.asset(
                  'assets/images/next_arrow.png',
                  width: 30,
                ),
              ),
            ],
          ),
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            height: 10,
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 30),
            child: Text(
              '你好，欢迎加入汇房云',
              style: TextStyle(
                  fontSize: 22,
                  color: Color(0xFF333333),
                  fontWeight: FontWeight.w500),
            ),
          ),
          SizedBox(
            height: 15,
          ),
          Form(
            key: _formKey,
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 30),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      TextFormField(
                        maxLines: 1,
                        keyboardAppearance: Brightness.light,
                        decoration: InputDecoration(
                          hintText: '请输入账号',
                          hintStyle: TextStyle(color: Color(0xFF999999)),
                        ),
                        onSaved: (String value) {
                          _account = value;
                        },
                        validator: (String value) {
                          return value.length == 11 ? null : "请输入正确的手机号码";},
                      ),
                      TextFormField(
                        maxLines: 1,
                        obscureText: true,
                        controller: TextEditingController(text: '测试'),
                        keyboardAppearance: Brightness.light,
                        decoration: InputDecoration(
                          hintText: '请输入密码',
                          hintStyle: TextStyle(color: Color(0xFF999999)),
                        ),
                        onSaved: (String value) {
                          _pwd = value;
                        },
                        validator: (String value) {
                          return value.length < 2 ? "请输入正确的密码" : null;
                        },
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      ConstrainedBox(
                        constraints:
                            BoxConstraints(minWidth: 300, minHeight: 45),
                        child: OutlinedButton(
                          style: ButtonStyle(
                            backgroundColor:
                                MaterialStateProperty.all(Colors.red),
                            textStyle: MaterialStateProperty.all(
                              TextStyle(
                                color: Colors.red,
                                fontSize: 16,
                              ),
                            ),
                          ),
                          onPressed: requestLogin,
                          child: Text(
                            '登录',
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: 300,
                        child: OutlinedButton(
                          style: ButtonStyle(
                            backgroundColor:
                                MaterialStateProperty.all(Colors.red),
                          ),
                          onPressed: () {},
                          child: Text('注册'),
                        ),
                      ), // TextField(),
                    ],
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}

class UserInheritedWidget extends InheritedWidget {
  static UserInheritedWidget of(BuildContext context, {bool rebuild}) {
    return context
        .getElementForInheritedWidgetOfExactType<UserInheritedWidget>()
        .widget;
  }

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return false;
  }
}
